<template>
    <div class="app-container">
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <span>发布公告</span>
        </div>
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="标题" prop="title">
            <el-input v-model="form.title" placeholder="请输入公告标题"></el-input>
          </el-form-item>
          <el-form-item label="公告类型" prop="type">
            <el-select v-model="form.type" placeholder="请选择公告类型">
              <el-option label="通知公告" value="notice"></el-option>
              <el-option label="规章制度" value="rule"></el-option>
              <el-option label="工作动态" value="dynamic"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="特殊标记">
            <el-checkbox v-model="form.isTop">置顶</el-checkbox>
            <el-checkbox v-model="form.isUrgent">紧急</el-checkbox>
          </el-form-item>
          <el-form-item label="有效期" prop="validPeriod">
            <el-date-picker
              v-model="form.validPeriod"
              type="daterange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="内容" prop="content">
            <el-input type="textarea" :rows="10" v-model="form.content" placeholder="请输入公告内容"></el-input>
          </el-form-item>
          <el-form-item label="附件">
            <el-upload
              class="upload-demo"
              action="https://jsonplaceholder.typicode.com/posts/"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :before-remove="beforeRemove"
              multiple
              :limit="3"
              :on-exceed="handleExceed"
              :file-list="fileList">
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png/pdf文件，且不超过10MB</div>
            </el-upload>
          </el-form-item>
          <el-form-item label="接收范围" prop="receivers">
            <el-select
              v-model="form.receivers"
              multiple
              placeholder="请选择接收范围">
              <el-option label="全公司" value="all"></el-option>
              <el-option label="研发部" value="dev"></el-option>
              <el-option label="市场部" value="market"></el-option>
              <el-option label="销售部" value="sales"></el-option>
              <el-option label="人事部" value="hr"></el-option>
              <el-option label="财务部" value="finance"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">立即发布</el-button>
            <el-button @click="onCancel">取消</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </template>
  
  <script>
  export default {
    name: 'CreateNotice',
    data() {
      return {
        form: {
          title: '',
          type: '',
          isTop: false,
          isUrgent: false,
          validPeriod: [],
          content: '',
          receivers: []
        },
        rules: {
          title: [
            { required: true, message: '请输入公告标题', trigger: 'blur' },
            { min: 3, max: 50, message: '长度在 3 到 50 个字符', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '请选择公告类型', trigger: 'change' }
          ],
          validPeriod: [
            { type: 'array', required: true, message: '请选择有效期', trigger: 'change' }
          ],
          content: [
            { required: true, message: '请输入公告内容', trigger: 'blur' }
          ],
          receivers: [
            { type: 'array', required: true, message: '请选择接收范围', trigger: 'change' }
          ]
        },
        fileList: []
      }
    },
    methods: {
      handleRemove(file, fileList) {
        console.log(file, fileList)
      },
      handlePreview(file) {
        console.log(file)
      },
      handleExceed(files, fileList) {
        this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`)
      },
      beforeRemove(file, fileList) {
        return this.$confirm(`确定移除 ${file.name}？`)
      },
      onSubmit() {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.$message({
              message: '发布成功',
              type: 'success'
            })
            // 实际项目中应该调用API
            // createNotice(this.form).then(() => {
            //   this.$message({
            //     message: '发布成功',
            //     type: 'success'
            //   })
            //   this.$router.push('/notice/list')
            // })
            this.$router.push('/notice/list')
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      onCancel() {
        this.$router.push('/notice/list')
      }
    }
  }
  </script>

  <style scoped>
  .app-container {
    padding: 20px;
  }
  </style>